
#include "Common.h"

uchar charToUChar(char c)
{
    return uchar(c);
}

int charToInt(char c)
{
    return int(charToUChar(c));
}

long long charToLongLong(char c)
{
    return (long long)(charToUChar(c));
}

char ReadByteFromDataStream(QDataStream *streamPtr)
{
    char c;
    streamPtr->readRawData(&c, 1);

    return c;
}

void ReadAllFromDataStream(QDataStream *streamPtr, QByteArray &ba)
{
    while(!streamPtr->atEnd()) {
        char c;
        streamPtr->readRawData(&c, 1);
        ba.append(c);
    }
}

QByteArray ReadFromDataStream(QDataStream *streamPtr, uint len)
{
    QByteArray ba;
    for (uint i = 0; i < len; i++) {
        const char &c = ReadByteFromDataStream(streamPtr);
        ba.append(c);
    }

    return ba;
}

int ReadInt(QDataStream *streamPtr)
{
    char c = ReadByteFromDataStream(streamPtr);
    uint intValue = charToInt(c);
    c = ReadByteFromDataStream(streamPtr);
    intValue += charToInt(c) << 8;
    c = ReadByteFromDataStream(streamPtr);
    intValue += charToInt(c) << 16;
    c = ReadByteFromDataStream(streamPtr);
    intValue += charToInt(c) << 24;

    return intValue;
}

long long ReadLongLong(QDataStream *streamPtr)
{
    char c = ReadByteFromDataStream(streamPtr);;
    long long longLongValue = charToLongLong(c);
    c = ReadByteFromDataStream(streamPtr);
    longLongValue += charToLongLong(c) << 8;
    c = ReadByteFromDataStream(streamPtr);
    longLongValue += charToLongLong(c) << 16;
    c = ReadByteFromDataStream(streamPtr);
    longLongValue += charToLongLong(c) << 24;
    c = ReadByteFromDataStream(streamPtr);
    longLongValue += charToLongLong(c) << 32;
    c = ReadByteFromDataStream(streamPtr);
    longLongValue += charToLongLong(c) << 40;
    c = ReadByteFromDataStream(streamPtr);
    longLongValue += charToLongLong(c) << 48;
    c = ReadByteFromDataStream(streamPtr);
    longLongValue += charToLongLong(c) << 56;

    return longLongValue;
}

void GetImgFromUCharArray(QImage &img, int w, int h, uchar * const data, int dataLength, SpriteTypeEnum type)
{
    img = QImage(w, h, QImage::Format::Format_ARGB32);
    int r, g, b, a;
    // 颜色单元占用的字节数
    int colorUnitByteCount = 0;
    if (type == SpriteTypeEnum::Argb8888) {
        colorUnitByteCount = 4;
    } else {
        colorUnitByteCount = 2;
    }

    if (type == SpriteTypeEnum::Argb8888 && 4 > dataLength) {
        return;
    }
    if (type != SpriteTypeEnum::Argb8888 && 2 > dataLength) {
        return;
    }
    uchar *dataPtr = data;
    for (int y = 0; y < img.height(); y++) {
        for (int x = 0; x < img.width(); x++) {
            switch (type) {
            case Argb1555:
                a = dataPtr[1] >> 7;
                r = (dataPtr[1] >> 2) & 0x1f;
                g = (dataPtr[0] >> 5) | ((dataPtr[1] & 3) << 3);
                b = dataPtr[0] & 0x1f;
                a = a * 0xff;
                r = (r << 3) | (r >> 2);
                g = (g << 3) | (g >> 2);
                b = (b << 3) | (b >> 2);
                a = qMax(r, qMax(g, b));
                break;
            case Argb4444:
                a = dataPtr[1] & 0xf0;
                r = (dataPtr[1] & 0xf) << 4;
                g = dataPtr[0] & 0xf0;
                b = (dataPtr[0] & 0xf) << 4;
                break;
            case Argb8888:
                a = (dataPtr[3]);
                r = (dataPtr[2]);
                g = (dataPtr[1]);
                b = (dataPtr[0]);
                break;
            default:
                a = r = g = b = 0;
                break;
            }

            img.setPixelColor(x, y, QColor(r, g, b, a));
            dataPtr += colorUnitByteCount;
        }
    }
}

